package com.xjgc.eip.twice.zxjEip2cbhwdzs.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.xjgc.common.constant.Consants;
import com.xjgc.eip.twice.zxjEip2cbhwdzs.service.ZxjEip2cBhwdzsService;
import com.xjgc.eip.twice.zxjeip2csjjcss.entity.ZxjEip2cSjjcss;
import com.xjgc.eip.twice.zxjeip2csjjcss.mapper.ZxjEip2cSjjcssMapper;
import com.xjgc.eip.twice.zxjeip2cwdjcss.entity.ZxjEip2cWdjcss;
import com.xjgc.eip.twice.zxjeip2cwdjcss.mapper.ZxjEip2cWdjcssMapper;

/**
 * <p>
 *  波峰焊回流焊温度追溯
 * </p>
 *
 * @author XXX
 * @since 2019-12-06
 */
@Service
public class ZxjEip2cBhwdzsServiceImpl extends ServiceImpl<ZxjEip2cSjjcssMapper, ZxjEip2cSjjcss> implements ZxjEip2cBhwdzsService {
    @Autowired
    ZxjEip2cSjjcssMapper zxjEip2cSjjcssMapper;

    @Autowired
    ZxjEip2cWdjcssMapper zxjEip2cWdjcssMapper;



    @Override
    public List<Map<String, Object>> getBhwdzsData(Integer amount, String supplierCode) {
        //在温区时间查询出符合条件的BOARD_NO信息
        QueryWrapper<ZxjEip2cSjjcss> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("DISTINCT smt_bkbh").eq("ioflag", Consants.Unread)
                    .groupBy("smt_bkbh")
                    .last("limit " + (amount == null ? 100 : amount));
        if(StringUtils.isNotBlank(supplierCode)){
            queryWrapper.eq("supplier_code",supplierCode);
        }
        List<ZxjEip2cSjjcss> result = zxjEip2cSjjcssMapper.selectList(queryWrapper);//查询出

        if (result!=null&&result.size()>0){
            //根据温区时间查询出符合条件的BOARD_NO信息来批量在温区时间表中查询出温区数据
            List<String> boardNos= Lists.newArrayList();
            result.forEach(item->{
                boardNos.add(item.getBoardNo());
            });
            QueryWrapper<ZxjEip2cSjjcss> queryWrappers = new QueryWrapper<>();
            queryWrappers.in("smt_bkbh",boardNos);
            List<ZxjEip2cSjjcss> sjjcss = zxjEip2cSjjcssMapper.selectList(queryWrappers);
            //根据温区时间查询出符合条件的BOARD_NO信息来批量在温度表中查询出温度数据
            QueryWrapper<ZxjEip2cWdjcss> queryWrapperWdjcss = new QueryWrapper<>();
            queryWrapperWdjcss.in("smt_bkbh",boardNos).eq("ioflag", Consants.Unread);
            List<ZxjEip2cWdjcss> wdjcss = zxjEip2cWdjcssMapper.selectList(queryWrapperWdjcss);
            Map<String,Object> map=null;//保存过滤出来的温区和温度时间信息
            List<Object> BOARD_NO=null;//保存过滤出来的温区时间信息
            List<Object> temperatures=null;//保存过滤出来的温度时间信息
            List<Map<String,Object>> data=Lists.newArrayList();//保存构造好的数据返回国网
            for (ZxjEip2cSjjcss zxjEip2cSjjcss:result){
                map= Maps.newHashMap();
                BOARD_NO=Lists.newArrayList();
                temperatures=Lists.newArrayList();

                for (ZxjEip2cSjjcss zxjEip2cSjjcsso:sjjcss){
                    if (zxjEip2cSjjcsso.getBoardNo().equals(zxjEip2cSjjcss.getBoardNo())){
                        BOARD_NO.add(zxjEip2cSjjcsso);
                    }
                }
                if (wdjcss!=null&&wdjcss.size()>0){
                    for (ZxjEip2cWdjcss zxjEip2cWdjcss:wdjcss){
                        if (zxjEip2cWdjcss.getBoardNo().equals(zxjEip2cSjjcss.getBoardNo())){
                            temperatures.add(zxjEip2cWdjcss);
                        }
                    }
                }
                map.put("BOARD_NO",BOARD_NO);
                map.put("temperatures",temperatures);
                data.add(map);
            }
            //更新温区时间数据状态为已读
            UpdateWrapper<ZxjEip2cSjjcss> updateWrapperSjjcss= new UpdateWrapper<ZxjEip2cSjjcss>();
            updateWrapperSjjcss.set("update_time",new Date());
            updateWrapperSjjcss.set("ioflag",Consants.read).in("smt_bkbh",boardNos);
            zxjEip2cSjjcssMapper.update(null,updateWrapperSjjcss);
            //更新温区温度数据状态为已读
            UpdateWrapper<ZxjEip2cWdjcss> updateWrapperWdjcss= new UpdateWrapper<ZxjEip2cWdjcss>();
            updateWrapperWdjcss.set("update_time",new Date());
            updateWrapperWdjcss.set("ioflag",Consants.read).in("smt_bkbh",boardNos);
            zxjEip2cWdjcssMapper.update(null,updateWrapperWdjcss);
            return data;
        }

        return null;
    }
}
